package com.lmt.service.impl;

import com.lmt.base.BaseService;
import com.lmt.base.ResultData;
import com.lmt.entity.search.ResourceUploadSearch;
import com.lmt.service.ResourceUploadService;
import com.lmt.util.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Date 2018/07/17
 *
 * @author MT-Lin
 */
@Service("ResourceUploadServiceImpl")
public class ResourceUploadServiceImpl extends BaseService implements ResourceUploadService {

	@Override
	public ResultData search(ResourceUploadSearch resourceUploadSearch) {
		StringBuffer sql = new StringBuffer();
		List<Object> parameters = new ArrayList<>();
		sql.append("SELECT ");
		sql.append("rt.designation AS designation, ");
		sql.append("c.designation AS classify, ");
		sql.append("rt.gmt_create AS uploadTime, ");
		sql.append("dd.designation AS auditStatus, ");
		sql.append("a.gmt_create AS auditGmtCreate, ");
		sql.append("a.gmt_modified AS gmtModified ");
		sql.append("FROM resource_temp AS rt, digital_dictionary AS dd, ");
		sql.append("audit AS a, resource_upload AS ru, classify AS c ");
		sql.append("WHERE ru.user_id = ? ");
		parameters.add(resourceUploadSearch.getUserId());
		sql.append("AND ru.resource_temp_id = rt.id AND a.obj_id = rt.id ");
		sql.append("AND a.status = dd.code AND rt.classify_id = c.id ");
		if (!StringUtils.isEmpty(resourceUploadSearch.getDesignation())) {
			sql.append("AND rt.designation LIKE ? ");
			parameters.add("%" + resourceUploadSearch.getDesignation() + "%");
		}
		Map<String, Object> result = this.paginationSearch(sql, resourceUploadSearch, parameters);
		return ResultUtil.newSuccess("上传记录查询成功", result);
	}
}
